﻿using System.Text;
using System.Security.Cryptography;
using System.IO;
using System;

namespace Jinndev {

    public static class AESHelper {

        public static readonly string DefaultKey = "12345678123456781234567812345678";
        public static readonly string DefaultIV = "1234567812345678";

        /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="key">加密密钥(key的长度为32)</param>
        /// <param name="iv">向量128位(Iv的字符串长度为16)</param>
        public static string Encrypt(string encryptString, string key, string iv) {
            byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);//得到需要加密的字节数组
            byte[] result = Encrypt(inputByteArray, key, iv);
            return Convert.ToBase64String(result);
        }

        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="key">加密密钥(key的长度为32)</param>
        /// <param name="iv">向量128位(Iv的字符串长度为16)</param>
        public static string Decrypt(string decryptString, string key, string iv) {
            byte[] inputByteArray = Convert.FromBase64String(decryptString);//得到需要加密的字节数组
            byte[] result = Decrypt(inputByteArray, key, iv);
            return Encoding.UTF8.GetString(result).Replace("\0", string.Empty);
        }

        /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="key">加密密钥(key的长度为32)</param>
        /// <param name="iv">向量128位(Iv的字符串长度为16)</param>
        public static byte[] Encrypt(byte[] inputByteArray, string key, string iv) {
            //分组加密算法
            SymmetricAlgorithm Aes = Rijndael.Create();
            //设置密钥及密钥向量
            Aes.Key = Encoding.UTF8.GetBytes(key);
            Aes.IV = Encoding.UTF8.GetBytes(iv);
            using (MemoryStream ms = new MemoryStream()) {
                using (CryptoStream cs = new CryptoStream(ms, Aes.CreateEncryptor(), CryptoStreamMode.Write)) {
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    byte[] cipherBytes = ms.ToArray();//得到加密后的字节数组
                    cs.Close();
                    ms.Close();
                    return cipherBytes;
                }
            }
        }

        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="key">加密密钥(key的长度为32)</param>
        /// <param name="iv">向量128位(Iv的字符串长度为16)</param>
        public static byte[] Decrypt(byte[] inputByteArray, string key, string iv) {
            SymmetricAlgorithm Aes = Rijndael.Create();
            Aes.Key = Encoding.UTF8.GetBytes(key);
            Aes.IV = Encoding.UTF8.GetBytes(iv);

            byte[] decryptBytes = new byte[inputByteArray.Length];
            using (MemoryStream ms = new MemoryStream(inputByteArray)) {
                using (CryptoStream cs = new CryptoStream(ms, Aes.CreateDecryptor(), CryptoStreamMode.Read)) {
                    cs.Read(decryptBytes, 0, decryptBytes.Length);
                    cs.Close();
                    ms.Close();
                }
            }
            return decryptBytes;
            //return Encoding.UTF8.GetString(decryptBytes); //返回字符串
        }

    }

}
